package com.prs.crm.action.official;

import java.io.File;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.official.DeptMail;
import com.prs.crm.domain.official.DeptMailAutograph;
import com.prs.crm.domain.official.DeptMailComment;
import com.prs.crm.domain.official.DeptMailNum;
import com.prs.crm.domain.official.DeptMailReceive;
import com.prs.crm.domain.official.PersonMailDel;
import com.prs.crm.domain.official.PersonalMail;
import com.prs.crm.domain.official.PersonalMailAutograph;
import com.prs.crm.domain.official.PersonalMailComment;
import com.prs.crm.domain.official.PersonalMailNum;
import com.prs.crm.domain.official.PersonalMailReceive;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.official.DeptMailAutographService;
import com.prs.crm.service.official.DeptMailCommentService;
import com.prs.crm.service.official.DeptMailNumService;
import com.prs.crm.service.official.DeptMailReceiveService;
import com.prs.crm.service.official.DeptMailService;
import com.prs.crm.service.official.PersonMailDelService;
import com.prs.crm.service.official.PersonalMailAutographService;
import com.prs.crm.service.official.PersonalMailCommentService;
import com.prs.crm.service.official.PersonalMailNumService;
import com.prs.crm.service.official.PersonalMailReceiveService;
import com.prs.crm.service.official.PersonalMailService;
import com.prs.crm.tools.ControlJMail;

public class PersonalMailAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private File upload;
	private String uploadFileName;
	private PersonService personService;
	private String receivePersonId;
	private String types;
	private PersonalMail personalMail;
	private PersonalMailService personalMailService;
	private PersonalMailReceive personalMailReceive;
	private PersonalMailReceiveService personalMailReceiveService; 
	private List<PersonalMailNum> personalMailNum;
	private PersonalMailNumService personalMailNumService;
	private List<PersonalMailAutograph> personalMailAutograph;
	private PersonalMailAutographService personalMailAutographService;
	private List<PersonalMailComment> comment;
	private PersonalMailComment personalMailComment;
	private PersonalMailCommentService personalMailCommentService;
	private PersonalMailAutograph autograph;
	private Integer personalMailId;
	private ControlJMail cj=new ControlJMail();
	private Integer[] receiveIds;
	private Integer[] delIds;
	private PersonMailDel personMailDel;
	private PersonMailDelService personMailDelService;

	public PersonMailDel getPersonMailDel() {
		return personMailDel;
	}
	public void setPersonMailDel(PersonMailDel personMailDel) {
		this.personMailDel = personMailDel;
	}
	public PersonMailDelService getPersonMailDelService() {
		return personMailDelService;
	}
	public void setPersonMailDelService(PersonMailDelService personMailDelService) {
		this.personMailDelService = personMailDelService;
	}
	public ControlJMail getCj() {
		return cj;
	}
	public void setCj(ControlJMail cj) {
		this.cj = cj;
	}
	public Integer[] getReceiveIds() {
		return receiveIds;
	}
	public void setReceiveIds(Integer[] receiveIds) {
		this.receiveIds = receiveIds;
	}
	public Integer[] getDelIds() {
		return delIds;
	}
	public void setDelIds(Integer[] delIds) {
		this.delIds = delIds;
	}
	public Integer getPersonalMailId() {
		return personalMailId;
	}
	public void setPersonalMailId(Integer personalMailId) {
		this.personalMailId = personalMailId;
	}
	public PersonalMailAutograph getAutograph() {
		return autograph;
	}
	public void setAutograph(PersonalMailAutograph autograph) {
		this.autograph = autograph;
	}
	public List<PersonalMailComment> getComment() {
		return comment;
	}
	public void setComment(List<PersonalMailComment> comment) {
		this.comment = comment;
	}
	public PersonalMailComment getPersonalMailComment() {
		return personalMailComment;
	}
	public void setPersonalMailComment(PersonalMailComment personalMailComment) {
		this.personalMailComment = personalMailComment;
	}
	public PersonalMailCommentService getPersonalMailCommentService() {
		return personalMailCommentService;
	}
	public void setPersonalMailCommentService(
			PersonalMailCommentService personalMailCommentService) {
		this.personalMailCommentService = personalMailCommentService;
	}
	public List<PersonalMailAutograph> getPersonalMailAutograph() {
		return personalMailAutograph;
	}
	public void setPersonalMailAutograph(
			List<PersonalMailAutograph> personalMailAutograph) {
		this.personalMailAutograph = personalMailAutograph;
	}
	public PersonalMailAutographService getPersonalMailAutographService() {
		return personalMailAutographService;
	}
	public void setPersonalMailAutographService(
			PersonalMailAutographService personalMailAutographService) {
		this.personalMailAutographService = personalMailAutographService;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	public String getReceivePersonId() {
		return receivePersonId;
	}
	public void setReceivePersonId(String receivePersonId) {
		this.receivePersonId = receivePersonId;
	}
	public String getTypes() {
		return types;
	}
	public void setTypes(String types) {
		this.types = types;
	}

	public PersonalMail getPersonalMail() {
		return personalMail;
	}
	public void setPersonalMail(PersonalMail personalMail) {
		this.personalMail = personalMail;
	}
	public PersonalMailService getPersonalMailService() {
		return personalMailService;
	}
	public void setPersonalMailService(PersonalMailService personalMailService) {
		this.personalMailService = personalMailService;
	}
	public PersonalMailReceive getPersonalMailReceive() {
		return personalMailReceive;
	}
	public void setPersonalMailReceive(PersonalMailReceive personalMailReceive) {
		this.personalMailReceive = personalMailReceive;
	}
	public PersonalMailReceiveService getPersonalMailReceiveService() {
		return personalMailReceiveService;
	}
	public void setPersonalMailReceiveService(
			PersonalMailReceiveService personalMailReceiveService) {
		this.personalMailReceiveService = personalMailReceiveService;
	}
	public List<PersonalMailNum> getPersonalMailNum() {
		return personalMailNum;
	}
	public void setPersonalMailNum(List<PersonalMailNum> personalMailNum) {
		this.personalMailNum = personalMailNum;
	}
	public PersonalMailNumService getPersonalMailNumService() {
		return personalMailNumService;
	}
	public void setPersonalMailNumService(
			PersonalMailNumService personalMailNumService) {
		this.personalMailNumService = personalMailNumService;
	}
	@Action("listSendPersonalMail")
	public String listSendPersonalMail(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(PersonalMail.class);
		criteria.add(Restrictions.eq("type", 1)).add(Restrictions.eq("recorder.id",  AppContext.getAccountContext().getUser().getId()));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPersonalMailService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("listReceivePersonalMail")
	public String listReceivePersonalMail(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(PersonalMailReceive.class);
		criteria.add(Restrictions.eq("type", 1)).add(Restrictions.eq("receivePerson.id", AppContext.getAccountContext().getUser().getId()));	
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPersonalMailReceiveService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("listRemovePersonalMail")
	public String listRemovePersonalMail(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(PersonMailDel.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPersonMailDelService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("listDraftPersonalMail")
	public String listDraftPersonalMail(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(PersonalMail.class);
		criteria.add(Restrictions.eq("type",2)).add(Restrictions.eq("recorder.id",AppContext.getAccountContext().getUser().getId()));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPersonalMailService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("viewPersonalMail")
	public String viewPersonalMail(){
		this.personalMailNum=this.getPersonalMailNumService().getAllPersonalMailNum();
		personalMail.setLastPerson(AppContext.getAccountContext().currentUser().getResume().getName());
		personalMail.setLastTime(new Date());
		this.getPersonalMailService().submit(personalMail);
		return SUCCESS;
	}
	@Action("editPersonalMail")
	public String editPersonalMail(){
		if(ids!=null){
			this.personalMail=this.getPersonalMailService().get(ids[0]);
		}
		if (receiveIds!=null) {
			this.personalMailReceive=this.getPersonalMailReceiveService().get(receiveIds[0]);
			this.personalMail=this.getPersonalMailService().getPersonMailById(personalMailReceive.getPersonalMailId());
		}
		this.personalMailNum=this.getPersonalMailNumService().getAllPersonalMailNum();
		this.personalMailAutograph=this.getPersonalMailAutographService().getAllPersonalMailAutograph();
		return SUCCESS;
	}
	@Action(value = "sendPersonalMail", results = {
			@Result(name = "success", type = "redirect", location = "editPersonalMail"),
			@Result(name = "error", type = "chain", location = "editPersonalMail") })
	public String sendPersonalMail(){
		if(upload!=null){
			this.personalMail.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		if (personalMail.getId()!=null) {
			if (personalMail.getType()==1) {
			this.addActionError("该邮件已经发送不需要再次发送");
			return ERROR;
			}
		}
		personalMail.setComment(0);
		personalMail.setType(1);
		personalMail.setUpdateTime(new Date());
		personalMail.setReceivePerosnId(receivePersonId);
		this.getPersonalMailService().save(personalMail);
		Collection<Person> acceptPerson=this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(personalMail.getReceivePerosnId().split(",")));
		Iterator<Person> iter=acceptPerson.iterator();
		while (iter.hasNext()) {
			Person person =iter.next();
			PersonalMailReceive dr=new PersonalMailReceive();
			dr.setPersonalMailId(personalMail.getId());
			dr.setType(1);
			dr.setTitle(personalMail.getTitle());
			dr.setContent(personalMail.getContent());
			dr.setMailType(personalMail.getMailType());
			if (personalMail.getAttach()!=null) {
				dr.setAttach(personalMail.getAttach());
			}
			dr.setComment(0);
			dr.setType(1);
			dr.setSendPerson(personalMail.getPersonalMailNum().getName());
			dr.setSendPersonAdress(personalMail.getPersonalMailNum().getMailAdress());
			dr.setChaoPerson(personalMail.getCopyToPerson());
			dr.setReceivePerson(person);
			dr.setState("未读");
			this.getPersonalMailReceiveService().save(dr);
			Person per=	this.getPersonService().get(dr.getReceivePerson().getId());
			Person curper=AppContext.getAccountContext().getUser();
			/**
	    	 * 发送邮件
	    	 * 
	    	 * @param from
	    	 *            邮件发送人的邮件地址
	    	 * @param to
	    	 *            邮件接收人的邮件地址
	    	 * @param username
	    	 *            发件人的邮件帐户
	    	 * @param password
	    	 *            发件人的邮件密码
	    	 * @param object
	    	 *            邮件标题
	    	 * @param content
	    	 *            邮件内容
	    	 */
			cj.sendMail(curper.getResume().getEmail(), per.getResume().getEmail(), "admin", "admin", dr.getTitle(), dr.getContent());
		}
		return SUCCESS;
	}
	@Action(value = "sendPersonalMails", results = {
			@Result(name = "success", type = "redirect", location = "listReceivePersonalMail"),
			@Result(name = "error", type = "chain", location = "editForwardPersonalMail") })
	public String sendPersonalMails(){
		if(upload!=null){
			this.personalMail.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		personalMail.setComment(0);
		personalMail.setType(1);
		personalMail.setUpdateTime(new Date());
		personalMail.setReceivePerosnId(receivePersonId);
		this.getPersonalMailService().save(personalMail);
		Collection<Person> acceptPerson=this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(personalMail.getReceivePerosnId().split(",")));
		Iterator<Person> iter=acceptPerson.iterator();
		while (iter.hasNext()) {
			Person person =iter.next();
			PersonalMailReceive dr=new PersonalMailReceive();
			dr.setPersonalMailId(personalMail.getId());
			dr.setType(1);
			dr.setTitle(personalMail.getTitle());
			dr.setContent(personalMail.getContent());
			dr.setMailType(personalMail.getMailType());
			if (personalMail.getAttach()!=null) {
				dr.setAttach(personalMail.getAttach());
			}
			dr.setComment(0);
			dr.setType(1);
			dr.setSendPerson(personalMail.getPersonalMailNum().getName());
			dr.setSendPersonAdress(personalMail.getPersonalMailNum().getMailAdress());
			dr.setChaoPerson(personalMail.getCopyToPerson());
			dr.setReceivePerson(person);
			dr.setState("未读");
			this.getPersonalMailReceiveService().save(dr);
			Person per=	this.getPersonService().get(dr.getReceivePerson().getId());
			Person curper=AppContext.getAccountContext().getUser();
			/**
	    	 * 发送邮件
	    	 * 
	    	 * @param from
	    	 *            邮件发送人的邮件地址
	    	 * @param to
	    	 *            邮件接收人的邮件地址
	    	 * @param username
	    	 *            发件人的邮件帐户
	    	 * @param password
	    	 *            发件人的邮件密码
	    	 * @param object
	    	 *            邮件标题
	    	 * @param content
	    	 *            邮件内容
	    	 */
			cj.sendMail(curper.getResume().getEmail(), per.getResume().getEmail(), "admin", "admin", dr.getTitle(), dr.getContent());
		}
		return SUCCESS;
	}
	@Action(value = "saveDraftPersonalMail", results = {
			@Result(name = "success", type = "redirect", location = "editPersonalMail"),
			@Result(name = "error", type = "chain", location = "editPersonalMail") })
	public String saveDraftPersonalMail(){
		if(upload!=null){
			this.personalMail.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		personalMail.setComment(0);
		personalMail.setType(2);
		personalMail.setUpdateTime(new Date());
		this.getPersonalMailService().save(personalMail);
		return SUCCESS;
	}
	@Action("viewPersonalMailReceive")
	public String viewFileTransmitReceive() {
		if(this.ids!=null){
			this.personalMailReceive=this.getPersonalMailReceiveService().get(ids[0]);
		}
		if(personalMailReceive.getState().equals("未读")){
			personalMailReceive.setState("已读");
		}
		personalMailReceive.setLastPerson(AppContext.getAccountContext().currentUser().getResume().getName());
		personalMailReceive.setLastTime(new Date());
		this.getPersonalMailReceiveService().submit(personalMailReceive);
		return SUCCESS;
	}
	@Action("editForwardPersonalMail")
	public String editForwardPersonalMail(){
		this.personalMailNum=this.getPersonalMailNumService().getAllPersonalMailNum();
		this.personalMailAutograph=this.getPersonalMailAutographService().getAllPersonalMailAutograph();
		if (delIds!=null) {
			this.personMailDel=this.getPersonMailDelService().get(delIds[0]);
			this.personalMail=personMailDel.getPersonalMail();
			this.personalMailReceive=personMailDel.getPersonalMailReceive();
		}	
		if (personalMail!=null) {
			personalMail.setContent("<BR>-----Original Message -----<BR>"+
					"Form:"+personalMail.getPersonalMailNum().getMailAdress()+";<BR>"+
					"To:"+personalMail.getReceivePerson()+"<BR>"+
					"Sent:"+personalMail.getRecordTime()+"<BR>"+
					"Subject:"+personalMail.getTitle()+"<BR>"+
					personalMail.getContent());
			personalMail.setTitle("FW:"+personalMail.getTitle());
		}
		if (personalMailReceive!=null) {
			personalMail=new PersonalMail();
			personalMail.setContent("<BR>-----Original Message -----<BR>"+
					"Form:"+personalMailReceive.getSendPersonAdress()+";<BR>"+
					"To:"+personalMailReceive.getReceivePerson()+"<BR>"+
					"Sent:"+personalMailReceive.getRecordTime()+"<BR>"+
					"Subject:"+personalMailReceive.getTitle()+"<BR>"+
					personalMailReceive.getContent());
			personalMail.setTitle("FW:"+personalMailReceive.getTitle());
		}
		if (ids!=null) {
			this.personalMail=this.getPersonalMailService().get(ids[0]);
			personalMail.setContent("<BR>-----Original Message -----<BR>"+
					"Form:"+personalMail.getPersonalMailNum().getMailAdress()+";<BR>"+
					"To:"+personalMail.getReceivePerson()+"<BR>"+
					"Sent:"+personalMail.getRecordTime()+"<BR>"+
					"Subject:"+personalMail.getTitle()+"<BR>"+
					personalMail.getContent());
			personalMail.setTitle("FW:"+personalMail.getTitle());
		}
		if (receiveIds!=null) {
			this.personalMailReceive=this.getPersonalMailReceiveService().get(receiveIds[0]);
			personalMail=new PersonalMail();
			personalMail.setContent("<BR>-----Original Message -----<BR>"+
					"Form:"+personalMailReceive.getSendPersonAdress()+";<BR>"+
					"To:"+personalMailReceive.getReceivePerson()+"<BR>"+
					"Sent:"+personalMailReceive.getRecordTime()+"<BR>"+
					"Subject:"+personalMailReceive.getTitle()+"<BR>"+
					personalMailReceive.getContent());
			personalMail.setTitle("FW:"+personalMailReceive.getTitle());
		}
		return SUCCESS;
	}
	@Action("editReplyPersonalMail")
	public String editReplyPersonalMail(){
		this.personalMailNum=this.getPersonalMailNumService().getAllPersonalMailNum();
		this.personalMailAutograph=this.getPersonalMailAutographService().getAllPersonalMailAutograph();
		if (personalMailReceive!=null) {
			PersonalMail pm=this.getPersonalMailService().getPersonMailById(personalMailReceive.getPersonalMailId());
			if (pm!=null) {
				if (types.equals("1")) {
					personalMail=new PersonalMail();
					personalMail.setContent("<BR>-----Original Message -----<BR>"+
							"Form:"+pm.getPersonalMailNum().getMailAdress()+";<BR>"+
							"To:"+pm.getReceivePerson()+"<BR>"+
							"Sent:"+pm.getRecordTime()+"<BR>"+
							"Subject:"+pm.getTitle()+"<BR>"+
							pm.getContent());
					personalMail.setTitle("Fe:"+pm.getTitle());
					this.receivePersonId=pm.getReceivePerosnId();
				}else{
					personalMail=new PersonalMail();
					String[] receivePerson=pm.getReceivePerson().split(",");
					String[] receiveId=pm.getReceivePerosnId().split(",");
					personalMail.setContent("<BR>-----Original Message -----<BR>"+
							"Form:"+pm.getPersonalMailNum().getMailAdress()+";<BR>"+
							"To:"+receivePerson[0]+"<BR>"+
							"Sent:"+pm.getRecordTime()+"<BR>"+
							"Subject:"+pm.getTitle()+"<BR>"+
							pm.getContent());
					personalMail.setTitle("Fe:"+pm.getTitle());
					this.receivePersonId=receiveId[0];
				}
			}
		}
		if (personalMail!=null) {
			if (types.equals("1")) {
				personalMail.setContent("<BR>-----Original Message -----<BR>"+
						"Form:"+personalMail.getPersonalMailNum().getMailAdress()+";<BR>"+
						"To:"+personalMail.getReceivePerson()+"<BR>"+
						"Sent:"+personalMail.getRecordTime()+"<BR>"+
						"Subject:"+personalMail.getTitle()+"<BR>"+
						personalMail.getContent());
				personalMail.setTitle("Fe:"+personalMail.getTitle());
				this.receivePersonId=personalMail.getReceivePerosnId();
			}else{
				String[] receivePerson=this.personalMail.getReceivePerson().split(",");
				String[] receiveId=this.personalMail.getReceivePerosnId().split(",");
				personalMail.setContent("<BR>-----Original Message -----<BR>"+
						"Form:"+personalMail.getPersonalMailNum().getMailAdress()+";<BR>"+
						"To:"+receivePerson[0]+"<BR>"+
						"Sent:"+personalMail.getRecordTime()+"<BR>"+
						"Subject:"+personalMail.getTitle()+"<BR>"+
						personalMail.getContent());
				personalMail.setTitle("Fe:"+personalMail.getTitle());
				this.receivePersonId=receiveId[0];
			}
		}
		if (ids!=null) {
			this.personalMail=this.getPersonalMailService().get(ids[0]);
			if (types.equals("1")) {
				personalMail.setContent("<BR>-----Original Message -----<BR>"+
						"Form:"+personalMail.getPersonalMailNum().getMailAdress()+";<BR>"+
						"To:"+personalMail.getReceivePerson()+"<BR>"+
						"Sent:"+personalMail.getRecordTime()+"<BR>"+
						"Subject:"+personalMail.getTitle()+"<BR>"+
						personalMail.getContent());
				personalMail.setTitle("Fe:"+personalMail.getTitle());
				this.receivePersonId=personalMail.getReceivePerosnId();
			}else{
				String[] receivePerson=this.personalMail.getReceivePerson().split(",");
				String[] receiveId=this.personalMail.getReceivePerosnId().split(",");
				personalMail.setContent("<BR>-----Original Message -----<BR>"+
						"Form:"+personalMail.getPersonalMailNum().getMailAdress()+";<BR>"+
						"To:"+receivePerson[0]+"<BR>"+
						"Sent:"+personalMail.getRecordTime()+"<BR>"+
						"Subject:"+personalMail.getTitle()+"<BR>"+
						personalMail.getContent());
				personalMail.setTitle("Fe:"+personalMail.getTitle());
				this.receivePersonId=receiveId[0];
			}
		}
		if (receiveIds!=null) {
			this.personalMailReceive=this.getPersonalMailReceiveService().get(receiveIds[0]);
			PersonalMail pm=this.getPersonalMailService().getPersonMailById(personalMailReceive.getPersonalMailId());
			if (pm!=null) {
				if (types.equals("1")) {
					personalMail=new PersonalMail();
					personalMail.setContent("<BR>-----Original Message -----<BR>"+
							"Form:"+pm.getPersonalMailNum().getMailAdress()+";<BR>"+
							"To:"+pm.getReceivePerson()+"<BR>"+
							"Sent:"+pm.getRecordTime()+"<BR>"+
							"Subject:"+pm.getTitle()+"<BR>"+
							pm.getContent());
					personalMail.setTitle("Fe:"+pm.getTitle());
					this.receivePersonId=pm.getReceivePerosnId();
				}else{
					personalMail=new PersonalMail();
					String[] receivePerson=pm.getReceivePerson().split(",");
					String[] receiveId=pm.getReceivePerosnId().split(",");
					personalMail.setContent("<BR>-----Original Message -----<BR>"+
							"Form:"+pm.getPersonalMailNum().getMailAdress()+";<BR>"+
							"To:"+receivePerson[0]+"<BR>"+
							"Sent:"+pm.getRecordTime()+"<BR>"+
							"Subject:"+pm.getTitle()+"<BR>"+
							pm.getContent());
					personalMail.setTitle("Fe:"+pm.getTitle());
					this.receivePersonId=receiveId[0];
				}
			}
		}
		return SUCCESS;
	}
	
	@Action(value = "removeSendPersonalMail", results = {
			@Result(name = "success", type = "redirect", location = "listSendPersonalMail"),
			@Result(name = "error", type = "chain", location = "listSendPersonalMail") })
	public String removeSendPersonalMail(){
		if (ids!=null) {
			this.personalMail=this.getPersonalMailService().get(ids[0]);
		}
		personalMail.setType(3);
		this.getPersonalMailService().submit(personalMail);
		personMailDel=new PersonMailDel();
		personMailDel.setPersonalMail(personalMail);
		this.getPersonMailDelService().save(personMailDel);
		return SUCCESS;
	}
	@Action(value = "removeSendPersonalMails", results = {
			@Result(name = "success", type = "redirect", location = "listSendPersonalMail"),
			@Result(name = "error", type = "chain", location = "listSendPersonalMail") })
	public String removeSendPersonalMails(){
		if (ids!=null) {
			this.personalMail=this.getPersonalMailService().get(ids[0]);
		}
		this.getPersonalMailService().batchRemove(ids);
		return SUCCESS;
	}
	@Action(value = "removeSendPersonalMails1", results = {
			@Result(name = "success", type = "redirect", location = "listSendPersonalMail"),
			@Result(name = "error", type = "chain", location = "listSendPersonalMail") })
	public String removeSendPersonalMails1(){
		if (ids!=null) {
			this.personalMail=this.getPersonalMailService().get(ids[0]);
		}
		this.getPersonalMailService().remove(personalMail);
		return SUCCESS;
	}
	@Action(value = "removePersonalMailsAll", results = {
			@Result(name = "success", type = "redirect", location = "listRemovePersonalMail"),
			@Result(name = "error", type = "chain", location = "listRemovePersonalMail") })
	public String removePersonalMailsAll(){
		if (delIds!=null) {
			this.personMailDel=this.getPersonMailDelService().get(delIds[0]);
		}
		if (personMailDel.getPersonalMail()!=null) {
			this.getPersonalMailService().remove(personMailDel.getPersonalMail());
		}
		if (personMailDel.getPersonalMailReceive()!=null) {
			this.getPersonalMailReceiveService().remove(personMailDel.getPersonalMailReceive());	
		}
		return SUCCESS;
	}
	@Action(value = "removeReceivePersonalMail", results = {
			@Result(name = "success", type = "redirect", location = "listReceivePersonalMail"),
			@Result(name = "error", type = "chain", location = "listReceivePersonalMail") })
	public String removeReceivePersonalMail(){
		if (ids!=null) {
			this.personalMailReceive=this.getPersonalMailReceiveService().get(ids[0]);
		}
		personalMailReceive.setType(3);
		this.getPersonalMailReceiveService().submit(personalMailReceive);
		personMailDel=new PersonMailDel();
		personMailDel.setPersonalMailReceive(personalMailReceive);
		this.getPersonMailDelService().save(personMailDel);
		return SUCCESS;
	}
	@Action(value = "removeReceivePersonalMails", results = {
			@Result(name = "success", type = "redirect", location = "listReceivePersonalMail"),
			@Result(name = "error", type = "chain", location = "listReceivePersonalMail") })
	public String removeReceivePersonalMails(){
		if (ids!=null) {
			this.personalMailReceive=this.getPersonalMailReceiveService().get(ids[0]);
		}
		this.getPersonalMailReceiveService().batchRemove(ids);
		return SUCCESS;
	}
	@Action(value = "removeReceivePersonalMails1", results = {
			@Result(name = "success", type = "redirect", location = "listReceivePersonalMail"),
			@Result(name = "error", type = "chain", location = "listReceivePersonalMail") })
	public String removeReceivePersonalMails1(){
		this.getPersonalMailReceiveService().remove(personalMailReceive);
		return SUCCESS;
	}
	
	@Action("viewPersonalMailComment")
	public String viewPersonalMailComment(){
		this.comment=this.personalMail.getPersonalMailComment();
		return SUCCESS;
	}
	@Action("viewPersonalMailReceiveComment")
	public String viewPersonalMailReceiveComment(){
		this.comment=this.personalMailReceive.getPersonalMailComment();
		return SUCCESS;
	}
	
	@Action(value = "savePersonalComment", results = {
			@Result(name = "success", type = "redirect", location = "viewPersonalMailComment?personalMail=${personalMail.getId()}"),
			@Result(name = "error", type = "chain", location = "viewPersonalMailComment?personalMail=${personalMail.getId()}") })
	public String savePersonalComment(){
		personalMail.setComment(personalMail.getComment()+1);
		this.getPersonalMailService().submit(personalMail);
		personalMailComment.setPersonalMail(personalMail);
		this.getPersonalMailCommentService().save(personalMailComment);
		return SUCCESS;
	}
	@Action(value = "savePersonalComments", results = {
			@Result(name = "success", type = "redirect", location = "viewPersonalMailReceiveComment?personalMailReceive=${personalMailReceive.getId()}"),
			@Result(name = "error", type = "chain", location = "viewPersonalMailReceiveComment?personalMailReceive=${personalMailReceive.getId()}") })
	public String savePersonalComments(){
		personalMailReceive.setComment(personalMailReceive.getComment()+1);
		this.getPersonalMailReceiveService().submit(personalMailReceive);
		personalMailComment.setPersonalMailReceive(personalMailReceive);
		this.getPersonalMailCommentService().save(personalMailComment);
		return SUCCESS;
	}
	
	
	@Action(value = "removeComment", results = {
			@Result(name = "success", type = "redirect", location = "viewPersonalMailReceiveComment?personalMailReceive=${personalMailComment.getPersonalMailReceive().getId()}"),
			@Result(name = "error", type = "chain", location = "viewPersonalMailReceiveComment?personalMailReceive=${personalMailComment.getPersonalMailReceive().getId()}") })
	public String removeComment(){
		this.getPersonalMailCommentService().remove(personalMailComment);
		personalMailComment.setPersonalMailReceive(personalMailComment.getPersonalMailReceive());
		personalMailComment.getPersonalMailReceive().setComment(personalMailComment.getPersonalMailReceive().getComment()-1);
		this.getPersonalMailReceiveService().submit(personalMailComment.getPersonalMailReceive());
		return SUCCESS;
	}
	
	
	@Action(value = "removeComments", results = {
			@Result(name = "success", type = "redirect", location = "viewPersonalMailComment?personalMail=${personalMailComment.getPersonalMail().getId()}"),
			@Result(name = "error", type = "chain", location = "viewPersonalMailComment?personalMail=${personalMailComment.getPersonalMail().getId()}") })
	public String removeComments(){
		this.getPersonalMailCommentService().remove(personalMailComment);
		personalMailComment.setPersonalMail(personalMailComment.getPersonalMail());
		personalMailComment.getPersonalMail().setComment(personalMailComment.getPersonalMail().getComment()-1);
		this.getPersonalMailService().submit(personalMailComment.getPersonalMail());
		return SUCCESS;
	}
	@Action(value="ajaxPersonalMailByid",results = { @Result(name = "success", type = "json",
			params = {"root", "autograph" }) })
	public String ajaxPersonalMailByid(){
		this.autograph=this.getPersonalMailAutographService().getPersonalMailAutographById(personalMailId);
		return SUCCESS;
	}
}
